<template>
  <el-row class="box">
    <el-col :lg="16" :sm="12" class="left-col">
      <div>
        <div class="left-title">欢迎光临</div>
        <div style="color: #a8adec">
          此站点是《vue3 + vite实战商城后台开发》视频课程的演示地址，
          <span>点击立即学习</span>
        </div>
      </div>
    </el-col>

    <el-col :lg="8" :sm="12" class="right-col">
      <div class="right-title">欢迎回来</div>
      <div class="right-sub-title">账号密码登录</div>

      <!-- 用户名 -->
      <el-form
        ref="userInfoRef"
        :model="userInfo" 
        :rules="userInfoRules" 
        style="width: 300px">
        <el-form-item prop="username">
          <el-input 
            v-model="userInfo.username" 
            placeholder="请输入用户名" 
            :prefix-icon="User"
          />
        </el-form-item>

        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input 
            v-model="userInfo.password"
            placeholder="请输入密码" 
            :prefix-icon="Lock"
            type="password"
            show-password
          />
        </el-form-item>

        <el-button type="primary" style="width: 100%;" size="large" round @click="loginFun(userInfoRef)">登录</el-button>
      </el-form>
    </el-col>
  </el-row>
</template>
<script setup>
import { reactive, ref, watch } from 'vue';
import { User, Lock } from '@element-plus/icons-vue'
import API from '@/api';
  const userInfoRef = ref()
  const userInfo = ref({
    username: '',
    password: ''
  })

  const userInfoRules= reactive({
    username: [
      { required: true, message: '请输入用户名', trigger: 'blur' },
      { min: 3, max: 10, message: '长度必须在 3-10 之间', trigger: 'blur' },
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 3, max: 10, message: '长度必须在 3-10 之间', trigger: 'blur' },
    ],
  })

  const loginFun = async (formEl) =>{
    await formEl.validate(async (valid,fields)=>{
      if(!valid) return
      try {
        console.log('登录请求数据:', userInfo.value);
        const res = await API.admin.login(userInfo.value)
        console.log('登录成功:', res);
        // 登录成功后的逻辑
      } catch (error) {
        console.error('登录失败:', error);
        console.error('错误响应:', error.response);
        if (error.response && error.response.status === 400) {
          console.error('400错误详情:', error.response.data);
        }
      }
    })
  }

</script> 

<style>
.box {
  min-width: 100vw;
  max-width: 100vw;
  min-height: 100vh;
  max-height: 100vh;
}
.left-col {
  background-color: #6e61f8;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.right-col {
  background-color: #ffffff;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.left-title {
  font-weight: 800;
  font-size: 50px;
  color: #ffffff;
}
.right-title{
  font-weight: 800;
  font-size: 50px
}

.right-sub-title{
  color: #DBDCDE;
  display: flex;
  align-items: center;
  margin: 20px 0 30px 0;
}

.right-sub-title::before{
  content: '';
  display: inline-block;
  height: 0px;
  width: 75px;
  margin-right: 10px;
  border: 0.5px solid #DDDFE6;
}

.right-sub-title::after{
  content: '';
  display: inline-block;
  height: 0px;
  width: 75px;
  margin-left: 10px;
  border: 0.5px solid #DDDFE6;
}
</style>
